package com.lzk.controller;

import com.lzk.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;



@RestController
@RequestMapping("order")
@DefaultProperties(defaultFallback = "getByIdFallback")//统一托底
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;


    /*@HystrixCommand(fallbackMethod = "getByIdFallback")//指定熔断方法*/
    @HystrixCommand //统一托底 需要调用的服务接口加HystrixCommand
    //通过order接口调用user接口
    @RequestMapping("getById/{id}")
    public User getById(@PathVariable("id") Integer id){

        //使用restTemplate发送请求调用接口
        String url = "http://user-server/user/getById/"+id;
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

   /* User getByIdFallback(@PathVariable("id") Integer id){
        return new User(-1,"熔断器开启","触发熔断");
           独立托底方法的参数和返回值要和被熔断方法一致
    }*/

    //统一托底方法，参数为空
    User getByIdFallback(){
        return new User(-1,"熔断器开启","触发熔断");
    }
}
